package com.cseg674.dataquery.authentication;


import java.util.*;
import java.sql.*;

public class Profile {

  private int user_id = 0;
  public void setuser_id(int user_id) {
    this.user_id = user_id;
  }
  public int getuser_id() {
    return user_id;
  }
  
  private String username = "";
  public void setUsername(String username) {
    this.username = username;
  }
  public String getUsername() {
    return username;
  }

  private String password = null;
  public void setPassword(String password) {
    this.password = password;
  }
  public String getPassword() {
    return password;
  }


  private String email = "";
  public void setEmail(String email) {
    this.email = email;
  }
  public String getEmail() {
    return email;
  }

  private String firstname = "";
  public void setFirstname(String firstname) {
    this.firstname = firstname;
  }
  public String getFirstname() {
    return firstname;
  }
  
  private String lastname = "";
  public void setLastname(String lastname) {
    this.lastname = lastname;
  }
  public String getLastname() {
    return lastname;
  }
  
  private String groupname = "";
  public void setGroupname(String groupname) {
    this.groupname = groupname;
  }
  public String getGroupname() {
    return groupname;
  }
  
  public int insert() throws Exception {
    if (!isValid())
      return Constants.FORM_ERROR;
    if (EmailExist())
      return Constants.FORM_ERROR;

    String insertsql =
        "insert into UI values (null,?,?,?,?,?,?)";

    Mysql mysql = new Mysql(insertsql);
    try {
      mysql.setString(1, this.username);
      mysql.setString(2, this.password);
      mysql.setString(3, this.firstname);
      mysql.setString(4, this.lastname);
      mysql.setString(5, this.groupname);
      mysql.setString(6, this.email);

      mysql.executeUpdate();

    } catch (Exception ex) {
      throw new Exception("Profile.insert()" + ex.getMessage());

    } finally {
      mysql.close();
      mysql = null;
    }
    return Constants.OK;
  }


  public int update() throws Exception {
    if (!isValid())
      return Constants.FORM_ERROR;
    char current = ' ';
    String updatesql = "update UI set " +
        "email=?,password=?,firstname=?,lastname=?,groupname=? where username=?";
    Mysql mysql = new Mysql(updatesql);
    try {

      
      mysql.setString(1, this.email);
      mysql.setString(2, this.password);
      mysql.setString(3, this.firstname);
      mysql.setString(4, this.lastname);
      mysql.setString(5, this.groupname);
      mysql.setString(6, username);

      mysql.executeUpdate();
    } catch (Exception ex) {
    	ex.printStackTrace();
      throw new Exception("Profile.update()" + ex.getMessage());
    } finally {
      mysql.close();
      mysql = null;
    }
    return Constants.OK;
  }


  public boolean select() throws Exception {
    String selectsql = "select * from UI where username = ?";
    Mysql mysql = new Mysql(selectsql);
    try {
      mysql.setString(1, username);
      ResultSet rs = mysql.executeQuery();
      boolean next = rs.next();
      if (next) {
        username = rs.getString("username");
        email = rs.getString("email");
        password = rs.getString("password");
        firstname = rs.getString("firstname");
        lastname = rs.getString("lastname");
        groupname = rs.getString("groupname");
      }
      rs.close();
      rs = null;
      return next;
    } catch (Exception ex) {
      throw new Exception("Profile.update()" + ex.getMessage());
    } finally {
      mysql.close();
      mysql = null;
    }
  }

  public boolean isValid() {
    boolean valid = true;
    if (username == null || username.equals(""))
      valid = false;
    if (username == null || username.equals(""))
      valid = false;

    return valid;
  }


  public boolean EmailExist() throws Exception {
    boolean exist = false;
    String sql = "select * from UI where email = ?";
    Mysql mysql = new Mysql(sql);
    try {
      mysql.setString(1, email);
      ResultSet rs = mysql.executeQuery();
      if (rs.next()) {
        exist = true;
      }
      rs.close();
      rs = null;
    } catch (Exception ex) {
      throw new Exception("Profile.update()" + ex.getMessage());
    } finally {
      mysql.close();
      mysql = null;
    }
    return exist;
  }

}